package com.example.weatherdemo.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.weatherdemo.R;
import com.example.weatherdemo.adapter.MyFragmentStVPTitleAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class VPHomeFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;

    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    private List<Fragment> mFragmentList;
    private List<String> mTitleList;
    private MyFragmentStVPTitleAdapter mStVpTitleAdapter;

    public VPHomeFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static VPHomeFragment newInstance(String param1, String param2) {
        VPHomeFragment fragment = new VPHomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_v_p_home, container, false);
    }

    /**
     * 当布局创建好了，就可以通过id找到viewPager和Tablayout了
     *
     * @param view               The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
     * @param savedInstanceState If non-null, this fragment is being re-constructed
     *                           from a previous saved state as given here.
     */
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mViewPager = (ViewPager) view.findViewById(R.id.home_vp);
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);

        initData();

        //添加Adapter
        mStVpTitleAdapter = new MyFragmentStVPTitleAdapter(getChildFragmentManager(),
                mFragmentList,
                mTitleList);

        mViewPager.setAdapter(mStVpTitleAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

    }

    private void initData() {
        mFragmentList = new ArrayList<>();

        VPFragment fragment1 = VPFragment.newInstance("位置1", "");
        VPFragment fragment2 = VPFragment.newInstance("位置2", "");
        VPFragment fragment3 = VPFragment.newInstance("位置3", "");
        VPFragment fragment4 = VPFragment.newInstance("位置4", "");
        VPFragment fragment5 = VPFragment.newInstance("位置5", "");
        VPFragment fragment6 = VPFragment.newInstance("位置6", "");
        VPFragment fragment7 = VPFragment.newInstance("位置7", "");

        mFragmentList.add(fragment1);
        mFragmentList.add(fragment2);
        mFragmentList.add(fragment3);
        mFragmentList.add(fragment4);
        mFragmentList.add(fragment5);
        mFragmentList.add(fragment6);
        mFragmentList.add(fragment7);

        mTitleList = new ArrayList<>();
        mTitleList.add("位置1");
        mTitleList.add("位置2");
        mTitleList.add("位置3");
        mTitleList.add("位置4");
        mTitleList.add("位置5");
        mTitleList.add("位置6");
        mTitleList.add("位置7");


    }
}